package com.zero.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JMSQueueProducer {

    public static void main(String[] args) {
        ConnectionFactory connectionFactory= new ActiveMQConnectionFactory("tcp://192.168.0.51:61616?jms.producerWindowSize=1048576");
        Connection connection=null;
        try {

            //设置消息的异步发送
            //((ActiveMQConnectionFactory)connectionFactory).setUseAsyncSend(true);
            connection=connectionFactory.createConnection();
            //((ActiveMQConnection)connection).setUseAsyncSend(true);
            connection.start();

            Session session=connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
            //创建目的地
            Destination destination=session.createQueue("myQueue");
            //创建发送者
            MessageProducer producer=session.createProducer(destination);
            //设定数据为持久化
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            //producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            for(int i=0; i<10; i++) {
                //创建需要发送的消息
                TextMessage message = session.createTextMessage("Hello World:"+i);
                //在发送端定义消息属性
                message.setStringProperty("wql","Hello wql");
                //设置消息属性
                message.setJMSMessageID("1");
                //Text   Map  Bytes  Stream  Object
                producer.send(message);
            }

            session.commit();
            session.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if(connection!=null){
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
